﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Isha.Data.Infrastructure;
using Isha.Entities;
using Isha.Repositories.Infrastructure;
using System.Data;

namespace Isha.Repositories
{
    internal class UserRepository : RepositoryBase<User>, IUserRepository
    {

        public UserRepository(IDatabaseFactory databaseFactory) 
            : base(databaseFactory)
        {

        }

        public void AssignRole(string userName, List<string> roleNames)
        {
            var user = this.Find(userName);
            user.Roles.Clear();
            foreach (string roleName in roleNames)
            {
                var role = this.DataContext.Roles.Find(roleName);
                user.Roles.Add(role);
            }

            this.DataContext.Users.Attach(user);
            this.DataContext.Entry(user).State = EntityState.Modified;
        }

        public User Find(string userName)
        {
            var user = base.Find(u => u.UserName == userName);
            return user;
        }

    }
}
